package com.teamone.etl.enterprise.controller;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.teamone.etl.enterprise.entity.Enterprise;
import com.teamone.etl.enterprise.request.EnterpriseRequest;
import com.teamone.etl.enterprise.response.ApiResponse;
import com.teamone.etl.enterprise.service.EnterpriseService;
import com.teamone.etl.exception.CustomException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 (Enterprise)表控制层

 @author makejava
 @since 2021-01-19 16:32:00 */
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController extends ApiController {
    /**
     服务对象
     */
    @Resource
    private EnterpriseService enterpriseService;

    @PostMapping("/list")
    public R<PageInfo<Enterprise>> getList(@RequestBody EnterpriseRequest request){
        return success(enterpriseService.getList(request));
    }

    @PutMapping("/update")
    public R<Boolean> update(@RequestBody EnterpriseRequest request) throws CustomException {
        return success(enterpriseService.updateWithSql(request) > 0);
    }

    @PostMapping("/add")
    public R<Boolean> add(@RequestBody EnterpriseRequest request) throws CustomException {
        return success(enterpriseService.add(request) > 0);
    }

    @PostMapping(value = "/uploadLicenseImage")
    public R<String> uploadLicenseImage(MultipartFile file) throws CustomException {
        return success(enterpriseService.uploadLicenseImage(file));
    }

    @PostMapping(value = "/uploadEnterpriseImage")
    public R<String> uploadEnterpriseImage(MultipartFile file) throws CustomException {
        return success(enterpriseService.uploadEnterpriseImage(file));
    }

    @PostMapping("/apiList")
    public R<PageInfo<ApiResponse>> apiList(@RequestBody EnterpriseRequest request){
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<ApiResponse> apis = enterpriseService.apiList(request.getId());
        return success(new PageInfo<>(apis));
    }

    @PostMapping("/login")
    public R<Boolean> login(@RequestBody EnterpriseRequest request) throws CustomException {
        return success(enterpriseService.login(request));
    }

    @PostMapping("/changePassword")
    public R<Boolean> changePassword(@RequestBody EnterpriseRequest request) throws CustomException {
        return success(enterpriseService.changePassword(request));
    }

}